Vue.component("special_question", {
	props: ['parent_data'],
	template: '<form class="form-horizontal form">\
				<template v-if=\'children_data.current_question.ID\'>\
					<form_com :parent_data="children_data"></form_com>\
				</template>\
				<div class="form-group">\
					<div class="col-sm-offset-3 col-sm-6">\
						<button type="button" style="width: 150px;" class="btn btn-base col-sm-3" @click="preStepCallback">{{preButtonTitle}}</button>\
						<button type="button" style="width: 150px;margin-left: 30px;" class="btn btn-base col-sm-3" @click="gonextquest">{{nextButtonTitle}}</button>\
					</div>\
				</div>\
			</form>',
	data: function() {
		return {
			url: {
				getSpecialQuestions: '/api/QuestionView/GetSpecialQuestions', //获取特殊问题
				saveAndLoadRecommendProducts: '/api/QuestionView/SaveAndLoadRecommendProducts', //提交专属问题
				getSpecialAnswers: '/api/UserQuestion/GetSpecialAnswers', //获取专属问题答案
				getAssetType: '/api/AssetTypeControl/GetPageAssetTypeControls', //获取离岸信托控件数据 
			},
			children_data: {
				component_data: {
					nextQuestionID: "", //下题ID 
					selectedValue: "", //当前问题的值 
					isStop: false,
					hasTip: false,
					ShowMsg: ''
				},
				control9_10_data: {
					parent_value: [],
					control_out: {
						err: "",
						result: "",
						all_values: ""
					}
				},
				current_question: {},
				currentIndex: 0 //组件中当前题是第几题
			},
			cModelId: "", //需要外面传进来
			CustomerID: "", //需要外面传进来
			parentObject: {},
			logStack: [],
			parentQuestionStack: [],
			waitProcessModelIds: [],
			preButtonTitle: '上一题',
			nextButtonTitle: '下一题',
			all_questions: [],
			all_models_answers: [],
			allAnswers: []
		};
	},
	created: function() {  
		
		var that = this;
		setTimeout(function() {
			//todo:ajax一次性请求所有问题数据            
			var data = {};
			data.ModelID = globalData.getStorage('modelID'); 
			if("IA"==globalData.getStorage('modelShortName')){
				data.ModelID = "";
			}
			
			globalData.ajaxPost(that.url.getSpecialQuestions, data, function(res) {
				if(res.suc) {
					that.all_questions = res.data;
					that.getModelIdsForSpecialQuestions();
					that.getFirstQuestion();
					//todo:获取题目答案，allAnswers
					if(that.parent_data.CustomerID) {
						globalData.ajaxPost(that.url.getSpecialAnswers, {
							'CustomerID': that.parent_data.CustomerID,
							'ModelID': globalData.getStorage("modelID")
						}, function(res) {
							if(res.suc) {
								$.each(res.data, function(index, item) {
									that.all_models_answers = item;
									that.setAnswerValues(item);
								})
							}
						})
					}
				}
			})

		}, 100);
	},
	mounted: function() {
		
	},
	watch: {},
	computed: {

	},
	methods: {
		//1在主界面，主要跟踪问题间的变化和状态数据
		//2在组件里，只关注问题里面的变化和状态数据
		//3综合1、2来获取答案并提交
		initComonentData: function() { 
			var valueType = this.children_data.current_question.ControlType == 4 ? [] : "";
			var data = {
				nextQuestionID: "", //下题ID 
				selectedValue: valueType, //当前问题的值 
				isStop: false,
				hasTip: false,
				ShowMsg: ''
			}; 
			this.children_data.component_data = data;
		},
		initControlData: function() {
			var data = {
				parent_value: [],
				control_out: {
					err: "",
					result: "",
					all_values: ""
				}
			};
			this.children_data.control9_10_data = data;
		},
		getFirstQuestion: function() {
			var me = this;
			if(me.waitProcessModelIds.length > 0) {
				me.cModelId = me.waitProcessModelIds[0];
			}
			$(me.all_questions).each(function(index, item) {
				if(item.IsFirst && item.ModelID == me.cModelId) {
					me.changeButtonTitle("上一步", "下一题");
					me.setCurrentQuestion(item);
					me.logStack.push(item);
//					alert("开始"+item.ModelName);
					return false;
				}
			});
		},
		preStepCallback: function() {
			var self = this;

			if(self.preButtonTitle=="上一步"){
				self.parent_data.question_step=1;
				return;
			}
			var preQuestion = self.logStack.pop();

			if(preQuestion != null) {
				if(preQuestion.IsFirst && self.logStack.length == 0) {
					self.changeButtonTitle("上一步", "下一题");
					self.logStack.push(preQuestion);
				}else{
					self.changeButtonTitle("上一题", "下一题");
				}

				if(preQuestion.ID == self.children_data.current_question.ID && self.logStack.length != 1) {
					self.preStepCallback();
				} else {
					self.setCurrentQuestion(preQuestion, self.allAnswers);
					//self.removeExistQuestionId(self.children_data.current_question.ID);
				}
				
			}else{
				self.changeButtonTitle("上一步", "下一题");
			}

		},
		removeExistQuestionId: function(questionId) {
			var self = this;
			for(var i = self.allAnswers.length; i > 0; i--) {
				if(self.allAnswers[i - 1].QuestionID == questionId) {
					self.allAnswers.splice(i - 1, 1);
				}
			}
		},
		//获取专属问题的答案
		getSpecialQuestionAnswers: function() {
			var self = this;
			var controlType = self.children_data.current_question.ControlType;

			//带文本框的控件
			if(controlType == "8") {
				var answers = {};
				answers.AnswerID = "";
				answers.QuestionID = self.children_data.current_question;
				answers.AnswerValue = self.children_data.component_data.selectedValue;
				answers.CustomerID = self.CustomerID;
				answers.ModelID = self.children_data.current_question.ModelID;
				self.removeExistQuestionId(answers.QuestionID);
				self.allAnswers.push(answers);

			} else if(controlType == "9" || controlType == "10") {
				var answers = {};
				var v = self.children_data.control9_10_data.control_out;
				$(self.children_data.current_question.AnswerOutputs).each(function(i, item) {
					if(v.result == item.AnswerItem) {
						answers.AnswerID = item.ID;
						answers.QuestionID = item.QuestionID;
						answers.CustomerID = self.CustomerID;
						answers.AnswerValue = v.all_values;
						answers.ModelID = self.children_data.current_question.ModelID;
					}
				});
				self.removeExistQuestionId(answers.QuestionID);
				self.allAnswers.push(answers);
			} else if(controlType == "4") {
				var removeOnce = true;
				$(self.children_data.component_data.selectedValue).each(function(i, item) {
					var answers = {};
					answers.AnswerID = item;
					answers.QuestionID = self.children_data.current_question.ID;
					answers.CustomerID = self.CustomerID;
					answers.ModelID = self.children_data.current_question.ModelID;
					if(removeOnce === true) {
						self.removeExistQuestionId(answers.QuestionID);
						removeOnce = false;
					}

					self.allAnswers.push(answers);
				});
			} else if(controlType == "3") {
				var answers = {};
				answers.AnswerID = self.children_data.component_data.selectedValue;
				answers.QuestionID = self.children_data.current_question.ID;
				answers.CustomerID = self.CustomerID;
				answers.ModelID = self.children_data.current_question.ModelID;
				self.removeExistQuestionId(answers.QuestionID);
				self.allAnswers.push(answers);
			}
		},
		getModelIdsForSpecialQuestions: function() {
			var self = this;
			$(self.all_questions).each(function(i, item) {
				var len = self.waitProcessModelIds.length;
				if(len == 0) {
					self.waitProcessModelIds.push(item.ModelID);
				} else if(item.ModelShortName !== "UA" && item.ModelShortName !== "TD") { //排除综合分析和财富健康的
					var bContains = self.containsModelId(item.ModelID, len);
					if(!bContains) {
						self.waitProcessModelIds.push(item.ModelID);
					}
				}
			});
		},
		containsModelId: function(modelId, modelIdsCount) {
			var self = this;
			var bContains = false;
			for(var i = 0; i < modelIdsCount; i++) {
				if(self.waitProcessModelIds[i] == modelId) {
					bContains = true;
					return true;
				}
			}
			return bContains;
		},
		findNextQuestions: function(cId) {
			var self = this;
			var rdata = null;
			var len = self.waitProcessModelIds.length;

			if(typeof cId == "object") {
				for(var i = 0; i < len; i++) {
					if(cId.ModelID == self.waitProcessModelIds[i]) {
						if(i + 1 < len)
							self.cModelId = self.waitProcessModelIds[i + 1];
					}
				}
			}

			$(self.all_questions).each(function(i, item) {
				if(len == 0) return false;

				if(self.cModelId == "") {
					self.cModelId = item.ModelID;
				}
				if(self.cModelId == item.ModelID) {
					if(typeof cId == "undefined" || typeof cId == "object") {
						if(item.IsFirst == true) {
							rdata = item;
							alert('开始' + item.ModelName);
							globalData.utils.alert() 
							return false;
						}
					} else {
						if(item.ID == cId) {
							rdata = item;
							return false;
						}
					}
				}

				return true;
			});
			return rdata;
		},
		//找出下一个答案
		findNextAnswer: function(cq, cIndex) {
			var self = this;
			var nId = '';
			$(self.parentObject.CheckedAnswers).each(function(j, obj) {
				$(cq.AnswerOutputs).each(function(i, item) {
					if(item.ID == obj.AnswerId && item.Index >= cIndex) {
						//更新cIndex
						self.children_data.currentIndex = item.Index;
						nId = obj.NextId;
						return false;
					}
					return true;
				});
				return nId == '';
			});
			var nQ = self.findNextQuestions(nId);
			return nQ;
		},
		//是否存在综合分析的下个模块的题 
		existNextQuestionInNewModel: function(cq) {
			var self = this;
			if(!cq) return false;
			var len = self.waitProcessModelIds.length;
			if(len > 0) {
				if(cq.ModelID == self.waitProcessModelIds[len - 1])
					return false;
			}
			return true;
		},
		//是否存在标题中配置的下一题 
		existNextQuestionInTitle: function(cq, cIndex) {
			if(!cq) return false;
			if(typeof cIndex == "undefined") cIndex = 0;
			var exist = false;
			if(cIndex == 0) { //如果是在标题中，则这个cIndex一定为0
				exist = typeof cq.NextQuestionID == "string" && cq.NextQuestionID !== "";
			}
			return exist;
		},

		//是否存在选项中配置的下一题
		existNextQuestionInAnswer: function(cq, cIndex) {
			if(!cq) return false;
			if(cq.AnswerOutputs) {
				if(typeof cIndex == "undefined") cIndex = 0;
				for(var i = cIndex; i < cq.AnswerOutputs.length; i++) {
					var nextQId = cq.AnswerOutputs[i].GotoQuestionID;
					if(typeof nextQId == "string" && nextQId != "") {
						return true;
					}
				}
			}
			return false;
		},
		travelNextQuestion: function(cq, cqIndex) {
			var self = this;
			var nextQuestion = null;
			if(self.existNextQuestionInTitle(cq, cqIndex)) { //标题上设置了下一题
				nextQuestion = self.findNextQuestions(cq.NextQuestionID);
			} else if(self.existNextQuestionInAnswer(cq, cqIndex)) { //答案上设置了下一题
				nextQuestion = self.findNextAnswer(cq, cqIndex);
			}

			//不存在下一题 ，返回父结点，看是否有兄弟结点存在下题
			//因为这里的递归不是连续的，每找到一个结果，就被IO中断，所以定义一个容器记住所有结点的父ID
			if(nextQuestion == null) {
				self.parentObject = self.parentQuestionStack.pop();
				if(self.parentObject != null) {
					//self.children_data.current_question = self.parentObject.Question;//这时当前问题变为父结点
					self.children_data.currentIndex = self.parentObject.TravelIndex + 1; //索引也移向下个答案
					//todo：这里需要找出选择答案的下题，选择的答案没有下题，则继续找下一个选项
					return self.travelNextQuestion(self.parentObject.Question, self.children_data.currentIndex);
				}
			}

			return nextQuestion;
		},
		nextStepCallback: function() {
			var self = this;
			self.parentObject = {};
			self.parentObject.CheckedAnswers = self.getCheckedAnswers();

			if(true == self.currentAnswerIsEnd()) {
				return;
			}
			var currentQuestionTemp = self.children_data.current_question;
			var cQuestion = self.travelNextQuestion(self.children_data.current_question, self.children_data.currentIndex);
			if(cQuestion == null) {
				//找出末尾的必做题
				cQuestion = self.getMustQuestion();
			}

			//是否存在下个版块的题
			if(cQuestion == null && self.existNextQuestionInNewModel(currentQuestionTemp)) {
				cQuestion = self.findNextQuestions(currentQuestionTemp);
			}

			if(cQuestion != null) {
				self.changeButtonTitle("上一题", "下一题");
				self.updateCurentQuestion(cQuestion);
			} else {
				//否则跳转到下一步
				self.changeButtonTitle("上一题", "完成");
			}
		},
		changeButtonTitle: function(pre, next) {
			this.preButtonTitle = pre;
			this.nextButtonTitle = next;
		},
		updateCurentQuestion: function(cQuestion) {
			var self = this;
			if(!self.parentObject) {
				self.parentObject = {};
			}
			self.parentObject.Question = self.children_data.current_question;
			self.parentObject.TravelIndex = self.children_data.currentIndex;
			self.parentQuestionStack.push(self.parentObject);
			self.logStack.push(cQuestion);

			self.children_data.currentIndex = 0;
			
			self.setCurrentQuestion(cQuestion, self.all_models_answers);
		},
		setCurrentQuestion: function(cQuestion, answers) {
			this.children_data.current_question = cQuestion;
			this.initComonentData();
			this.initControlData();

			this.getControl9Data();
			this.getControl10Data();
			this.setIPAge();
			this.setAnswerValues(answers);
		},
		setIPAge:function(){
			//设置国籍规划的年龄
			var self = this;
			var cQuestion = self.children_data.current_question;
			if(cQuestion.ControlType == 8) { //文本框
				var customerAge=globalData.getStorage("customerAge");
				self.children_data.component_data.selectedValue = customerAge;
			}
		},
		setAnswerValues: function(answers) {
			if(!answers) return;

			var self = this;
			var cQuestion = self.children_data.current_question;
			var cAnswers = [];
			$(answers).each(function(i, item) {
				if(item.QuestionID == cQuestion.ID) {
					if(cQuestion.ControlType == 3) { //单选
						self.children_data.component_data.selectedValue = item.AnswerID;
					} else if(cQuestion.ControlType == 4) { //多选
						if(item.AnswerID.indexOf(",") > -1) {
							$(item.AnswerID.split(',')).each(function(i, item) {
								cAnswers.push(item);
							});
						} else {
							cAnswers.push(item.AnswerID);
						}

					} else if(cQuestion.ControlType == 8) { //文本框
						self.children_data.component_data.selectedValue = item.AnswerValue;
					} else if(cQuestion.ControlType == 9 || cQuestion.ControlType == 10) { //离岸信托控件
						self.children_data.control9_10_data.control_out.result = item.AnswerID;
						self.children_data.control9_10_data.control_out.all_values = item.AnswerValue;
					}
				}
			});

			if(cQuestion.ControlType == 4) { //多选
				self.children_data.component_data.selectedValue = cAnswers;
			}
		},
		getMustQuestion: function() {
			var self = this;
			var returnQuestion = null;
			for(var i = self.logStack.length; i > 0; i--) {
				if(self.logStack[i - 1].IsMust == true) {
					returnQuestion = self.logStack[i - 1];
					break;
				}
			}
			if(returnQuestion != null)
				return null;

			$(this.all_questions).each(function(i, item) {
				if(item.IsMust == true) {
					returnQuestion = item;
					return false;
				}
			});
			return returnQuestion;
		},
		existSameNextId: function(nextId, checkedAnswers) {
			for(var i = checkedAnswers.length; i > 0; i--) {
				if(checkedAnswers[i - 1].NextId == nextId) {
					return true;
				}
			}
			return false;
		},
		getCheckedAnswers: function() {
			var me = this;
			var checkedAnswers = [];

			if(me.children_data.component_data.nextQuestionID) {
				var obj = {};
				obj.AnswerId = me.children_data.current_question.ID;
				obj.NextId = me.children_data.component_data.nextQuestionID;
					
				if(!me.existSameNextId(obj.NextId, checkedAnswers)) {
					checkedAnswers.push(obj);
				}
				return checkedAnswers;
			}
			
			var controlType = me.children_data.current_question.ControlType;
			if(controlType == 8) {
				var obj = {};
				obj.AnswerId = me.children_data.current_question.ID;
				obj.NextId = me.children_data.current_question.NextQuestionID;

				if(!me.existSameNextId(obj.NextId, checkedAnswers)) {
					checkedAnswers.push(obj);
				}
				return checkedAnswers;
			}

			var selectedValue=me.children_data.component_data.selectedValue;
			$(me.children_data.current_question.AnswerOutputs).each(function(aIndex, aItem) {  
				if(controlType==4){
					$(selectedValue).each(function(index, selectItem) {
						if(selectItem == aItem.ID) {
							var obj = {};
							obj.AnswerId = aItem.ID;
							obj.NextId = aItem.GotoQuestionID;
	
							if(!me.existSameNextId(obj.NextId, checkedAnswers)) {
								checkedAnswers.push(obj);
							}
						}
					});
				}else{
					if(selectedValue == aItem.ID) {
						var obj = {};
						obj.AnswerId = aItem.ID;
						obj.NextId = aItem.GotoQuestionID;

						if(!me.existSameNextId(obj.NextId, checkedAnswers)) {
							checkedAnswers.push(obj);
						}
					}
				}
				
			});
			return checkedAnswers;
		},
		currentAnswerIsEnd: function() {
			var self = this;
			if(self.children_data.component_data.hasTip) { //是否需要提示
				var msg = self.children_data.component_data.ShowMsg;
				alert(msg);
			}
			return self.children_data.component_data.isStop;
		},
		gonextquest: function() {
			var that = this;
			if(that.children_data.current_question.ControlType == "9" ||
				that.children_data.current_question.ControlType == "10") {
				if(that.children_data.control9_10_data.control_out.err) {
					alert(that.children_data.control9_10_data.control_out.err + "必选");
					return;
				}

				if(!that.children_data.control9_10_data.control_out.result) {
					alert("请答题");
					return;
				}
			} else if(!that.children_data.component_data.selectedValue || that.children_data.component_data.selectedValue.length == 0) {
				alert("请答题");
				return;
			}

			that.getSpecialQuestionAnswers();

			if(that.nextButtonTitle == "完成") {
				//修改答题步骤
				that.parent_data.question_step = 3
				globalData.utils.scrool_top()
				//todo:ajax将答案结果allAnswers提交
				$.each(that.allAnswers, function(index, item) {
					item.CustomerID = that.parent_data.CustomerID;
				})
				var data = {
					"Device": 3,
					"UserAnswers": [{
						"LoopTag": 1,
						"AnswerItems": that.allAnswers
					}]
				}

				globalData.ajaxPost(that.url.saveAndLoadRecommendProducts, data, function(res) {
					if(res.suc) {
						that.parent_data.evaluation_result = res.data; 
					}
				})
			} else {
				that.nextStepCallback();
			}
		},
		getControl9Data: function() {
			var self = this;
			//离岸信托的资产类型数据 
			if(self.children_data.current_question.ControlType == "9") {
				//todo:这里ajax请求 
				var data = {
					'Title': '资产类型列表',
					'PageSize': '1000',
					'PageIndex': '1'
				};
				globalData.ajaxPost(self.url.getAssetType, data, function(res) {
					if(res.suc) {
						self.children_data.control9_10_data.parent_value = res.data;
					}
				});
				setTimeout(function() {}, 200); //等待200毫秒
			}
		},
		getControl10Data: function() {
			var self = this;
			//离岸信托的债务状况数据
			if(self.children_data.current_question.ControlType == "10") {
				//todo:这里ajax请求 
				var data = {
					'Title': '债务状况',
					'PageSize': '1000',
					'PageIndex': '1'
				};
				globalData.ajaxPost(self.url.getAssetType, data, function(res) {
					if(res.suc) {
						self.children_data.control9_10_data.parent_value = res.data;
					}
				});
				setTimeout(function() {}, 200); //等待200毫秒
			}
		}
	}
})

Vue.component("form_com", {
	props: ['parent_data'],
	template: '<div class="itemQuestion">\
				<!--输入框-->\
				<div class="form-group" v-if="parent_data.current_question.ControlType==8">\
					<div class="col-sm-6 col-sm-offset-3">\
						<li class="title">{{parent_data.current_question.QuestionTitle}}？</li>\
						<div class="content">\
							<input type="number" class="form-control" name="workyear" v-model="parent_data.component_data.selectedValue" v-bind:placeholder="parent_data.current_question.PlaceHolder">{{parent_data.current_question.AfterTitle}}\
						</div>\
					</div>\
				</div>\
				<!-单选-->\
				<div class="form-group" v-if="parent_data.current_question.ControlType==3">\
					<div class="col-sm-6 col-sm-offset-3">\
						<li class="title">单选:{{parent_data.current_question.QuestionTitle}}？</li>\
						<div class="content">\
							<div class="checkbox" v-for="item in parent_data.current_question.AnswerOutputs">\
								<label @click="change_radio(item)">\
									<input type="radio" name="optionsRadios" v-model="parent_data.component_data.selectedValue" v-bind:value="item.ID">\
   									{{item.AnswerItem}}\
							</label>\
							</div>\
						</div>\
					</div>\
				</div>\
				<div class="form-group" v-if="parent_data.current_question.ControlType==4">\
					<div class="col-sm-6 col-sm-offset-3">\
						<li class="title">多选:{{parent_data.current_question.QuestionTitle}}？</li>\
						<div class="content">\
							<div class="checkbox" v-for="item in parent_data.current_question.AnswerOutputs">\
								<label @click="change_radio(item)">\
									<input type="checkbox" v-model="parent_data.component_data.selectedValue" v-bind:value="item.ID" >\
										{{item.AnswerItem}}\
								</label>\
							</div>\
						</div>\
					</div>\
				</div>\
				<template v-if="parent_data.control9_10_data.parent_value.length>0">\
					<div class="form-group" v-if="parent_data.current_question.ControlType==9||parent_data.current_question.ControlType==10">\
						<div class="col-sm-6 col-sm-offset-3">\
							<asset_control :control_out="parent_data.control9_10_data.control_out" :parent_value="parent_data.control9_10_data.parent_value" :control_type="parent_data.current_question.ControlType"></asset_control>\
						</div>\
					</div>\
				</template>\
			</div>',
	data: function() { 
		return {
			//value: this.getValue(this.parent_data.current_question.ControlType),
			selectItem: null
		}
	},
	computed: {
		isStop: function() {
			if(this.selectItem) {
				var IsLast = this.selectItem.IsLast;
				if(IsLast) {
					return true;
				}
			}
			return false;
		},
		hasTip: function() {
			if(this.selectItem) {
				var msg = this.selectItem.ShowMsg;
				if(msg) {
					this.parent_data.component_data.ShowMsg = msg;
					return msg.length > 0;
				}
			}
			return false;
		}
	},
	created:function(){ 
	},
	mounted: function() { 
	},
	watch: {
		// value: function (val, oldval) {
		// 	this.parent_data.component_data.selectedValue = val;
		// }
	},
	methods: {
		getValue: function(type) {
			var v = this.parent_data.component_data.selectedValue;
			if(v) {
				return v;
			}
			if(type == 4) {
				return [];
			} else {
				return "";
			}
		},
		change_radio: function(AnswerItem) {
			this.selectItem = AnswerItem;
			this.parent_data.component_data.isStop = this.isStop;
			this.parent_data.component_data.hasTip = this.hasTip;
		}
	} 
})

//离岸信托控件
Vue.component('asset_control', {
	template: '	<div><template v-for="(item1,index) in parent_value">\
				<div class="form-group" v-if="item1.Level==1">\
					<label for="exampleInputEmail1">{{item1.CategoryName}}</label>\
				</div>\
				<div class="form-group" v-for="(item2,index2) in parent_value" v-if="item2.ParentID==item1.ID">\
					<template v-if="control_type==9">\
						<label for="exampleInputEmail1">多选：{{item2.CategoryName}}</label>\
						<div>\
							<label class="checkbox-inline">\
								<input type="checkbox" v-model="checkbox_values" v-bind:id="item2.ID" v-bind:value="item2.ID+\'|A\'"> {{item2.LastTitle1}}\
							</label>\
							<label class="checkbox-inline">\
								<input type="checkbox" v-model="checkbox_values" v-bind:id="item2.ID"  v-bind:value="item2.ID+\'|B\'"> {{item2.LastTitle2}}\
							</label>\
						</div>\
					</template>\
					<template v-if="control_type==10">\
						<label for="exampleInputEmail1">单选：{{item2.CategoryName}}</label>\
						<div>\
							<label class="checkbox-inline">\
								<input type="radio" v-model="radio_values[index2]" v-bind:value="item2.ID+\'|A\'" > {{item2.LastTitle1}}\
							</label>\
							<label class="checkbox-inline">\
								<input type="radio" v-model="radio_values[index2]" v-bind:value="item2.ID+\'|B\'" > {{item2.LastTitle2}}\
							</label>\
						</div>\
					</template>\
				</div>\
			</template></div>',
	data: function() {
		return {
			selectedIds: "",
			checkbox_values: this.set_checkbox_data(),
			radio_values: this.set_radio_data()
		}
	},
	created:function(){ 
	},
	props: ['parent_value', "control_type", "control_out"],
	watch: {
		checkbox_values: function(val, oldval) {
			this.updateValue(val);
		},
		radio_values: function(val, oldval) {
			this.updateValue(val);
		}
	},
	methods: {
		set_checkbox_data: function() {
			var self = this;
			if(self.control_type == 9) { //多选
				var groupAnswers = self.control_out.all_values.split(';');
				var vals = [];
				$(groupAnswers).each(function(i, item) {
					if(item) {
						var nv = item.replace(',', '|').replace(',','');
						vals.push(nv);
					}
				});

				return vals;
			}
			return [];
		},
		set_radio_data: function() {
			if(this.control_type == 10) { //单选
				return this.convert_answers_to_options();
			}
			return [];
		},
		convert_answers_to_options: function() {
			var self = this;
			var groupAnswers = self.control_out.all_values.split(';');
			var vals = [];
			//遍历所有题，赋初值
			$(self.parent_value).each(function(i, item1) {
				if(item1.Level == 1) {
					$(self.parent_value).each(function(j, item2) {
						if(item1.ID == item2.ParentID) {
							vals.push(item2.ID);
						} else {
							vals.push("");
						}
					})
				}
			});

			//对应每个选项，给出对应答案，如果没有，则为空
			for(var index = vals.length; index > 0; index--) {
				var matchedValue = "";
				var id = vals[index - 1];
				if(id) {
					$(groupAnswers).each(function(i, item) {
						if(item) {
							var nv = item.replace(',', '|').replace(',','');
							if(nv.indexOf(id) > -1) {
								matchedValue = nv;
								return false;
							}
						}
					});
				}
				vals[index - 1] = matchedValue;
			}
			return vals;
		},
		updateValue: function(val) {
			var self = this;
			//题目规则
			var value = 0;
			self.selectedIds = "";
			$(val).each(function(index, item) {
				if(item.indexOf("|") > -1) {
					self.selectedIds += item.split('|')[0] + ",";
					var v = item.split('|')[1];
					if(!value)
						value = v;
					if(v == "B")
						value = v;
				}
			})

			self.control_out.err = "";
			self.control_out.result = value;
			self.control_out.all_values = self.removeEmpty(val);
			var msg = this.checkRequired();
			if(msg) {
				//进行赋值
				self.control_out.err = msg;
				self.control_out.result = "";
				self.control_out.all_values = "";
			}
		},
		removeEmpty: function(v) {
			var newVue = "";
			$(v).each(function(i, item) {
				if(item.indexOf("|") > -1) {
					newVue += item.replace('|', ',') + ";"
				}
			});
			return newVue;
		},
		checkRequired: function() {
			var self = this;
			var isSelected = false;
			var msg = "";
			var requiredIds = self.getRequiredIds();
			$(requiredIds).each(function(i, item) {

				isSelected = false;
				$(item.Ids).each(function(si, id) {
					if(self.selectedIds.indexOf(id) == -1) {
						isSelected = true;
						return false;
					}
				});

				if(isSelected) {
					msg = item.CategoryName;
					return false;
				}
			})

			if(isSelected) {
				return msg;
			}
			return "";
		},
		getRequiredIds: function() {
			var self = this;
			var requiredTypeIds = [];
			$(self.parent_value).each(function(i, item) {
				if(item.IsRequired) {
					requiredTypeIds.push(item);
				}
			});

			var requiredIds = [];
			$(requiredTypeIds).each(function(pIndex, pItem) {
				$(self.parent_value).each(function(i, item) {
					if(item.ParentID == pItem.ID) {
						var existPId = self.getExistPId(requiredIds, pItem.ID);
						if(!existPId) {
							existPId = {};
							existPId.ParentID = pItem.ID;
							existPId.CategoryName = pItem.CategoryName;
							existPId.Ids = [];
							requiredIds.push(existPId);
						}
						existPId.Ids.push(item.ID);
					}
				});
			});
			return requiredIds;
		},
		getExistPId: function(rIds, pid) {
			var bExist = null;
			$(rIds).each(function(i, item) {
				if(item.ParentID == pid) {
					bExist = item;
					return false;
				}
			});
			return bExist;
		}
	}
})